import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { MessageService } from 'src/app/core/service/message.service';
import { DatePipe } from '@angular/common';
 
/**
 * 首页
 */

@Component({
    templateUrl: './detail.html',
    styleUrls: ['./detail.scss'],
})
export class EquipmentDetailComponent implements OnInit {
    data:any = {};
    id;
    logList1 = [];
    logList2 = [];
    tableLoading1 = false;
    tableLoading2 = false;
    typeObj = {
        AntivirusSoftware:'杀毒软件', NetGAP:'硬件网闸', HardwareFirewall:'硬件防火墙'
    };
    ngOnInit() {
        this.id = this.activeRouter.snapshot.queryParams['id'];
        this.loadData();
    }
    constructor(
        private router: Router,
        private apiService: ApiService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService,
        private datePipe:DatePipe,
        private activeRouter:ActivatedRoute
    ) { }

    loadData(){
        this.apiService.post('接入设备详情',{id:this.id}).then((res:any)=>{
            this.data = res.data.detail;
            console.log(this.data)
            if(this.data){
                this.data.statusName = this.data.status === 'Normal' ? '正常':'失败';
                this.data.typeName = this.data.type ? this.typeObj[this.data.type]:"--";
            }
            // this.logList1 = this.data.userJoinDeviceLogList;
            // this.logList2 = this.data.dataMonitoringLogList;
        })
    }

    changeStatus(status){
        const content = status === 'Y'?'确认开启当前设备？':'确认关闭当前设备？';
        const message = status === 'Y'?'设备开启成功！':'设备关闭成功！';
        this.modal.confirm({
            nzTitle: '系统提示',
			nzContent: content,
			nzOkText: '确认',
			nzOkType: 'primary',
			nzOnOk: () => {
				return new Promise<void>((resolve, reject) => {
					this.apiService
						.post('开启/关闭接入设备', { id: this.id, enable: status })
						.then((res: any) => {
							resolve();
							this.nzMessageService.success(message);
							this.loadData();
						})
						.catch((res) => {
							reject();
							this.nzMessageService.warning(res);
						});
				}).catch(() => console.log('Oops errors!'));
			},
			nzCancelText: '取消',
        });
    }

    deleteEquipment(){
        this.modal.confirm({
            nzTitle: '系统提示',
			nzContent: '确认删除当前设备？',
			nzOkText: '删除',
			nzOkType: 'danger',
			nzOnOk: () => {
				return new Promise<void>((resolve, reject) => {
					this.apiService
						.post('删除接入设备', { id: this.id })
						.then((res: any) => {
							resolve();
							this.nzMessageService.success('设备删除成功！');
							this.router.navigateByUrl('equipment/detail');
						})
						.catch((res) => {
							reject();
							this.nzMessageService.warning(res);
						});
				}).catch(() => console.log('Oops errors!'));
			},
			nzCancelText: '取消',
        });
    }
    
}
